/**
* Author: Bob Chen
*/

package net.uman.framework.modules.search;

import java.util.List;

import net.uman.framework.modules.orm.PropertyFilter;
import net.uman.framework.modules.orm.PropertyFilter.MatchType;
import net.uman.framework.modules.orm.PropertyFilter.PropertyType;

public class Condition {
    public final static int EQUALS = 0;
    public final static int NOT_EQUALS = 1;
    public final static int LIKE = 2;
    public final static int GT = 3;
    public final static int LT = 4;
    public final static int GE = 5;
    public final static int LE = 6;
//    public final static int ISNULL = 5;
//    public final static int ISNOTNULL = 6;
    public final static int BETWEEN = 7;
    public final static int IN = 8;
    public final static int NOT_IN = 9;
    
    private String field;
    private Object[] values;
    private boolean valueField;
    
    int operator;
    
    Criteria criteria;

    public Condition() {
    }
    
    public Condition(String field, int operator, Object value) {
        this.field = field;
        this.operator = operator;
        if (value instanceof Object[]) {
        	this.values = (Object[])value;
        } else if (value instanceof List) {
        	this.values = ((List)value).toArray();
        } else {
        	this.values = new Object[]{value};
        }
    }

    public Condition(PropertyFilter filter) {
    	this(filter.getPropertyName(), getOperator(filter.getMatchType()), filter.getPropertyValue());
    	if (filter.getPropertyType() == PropertyType.F) {
    		valueField = true;
    	}
    }
    
    public Condition(Criteria criteria) {
        this.criteria = criteria;
    }
    
    public Criteria getCriteria() {
		return criteria;
	}

	public void setCriteria(Criteria criteria) {
		this.criteria = criteria;
	}

	public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public Object getValue() {
        return values == null || values.length == 0 ? null : values[0];
    }

    public Object[] getValues() {
        return values;
    }

    public void setValues(List values) {
		this.values = values.toArray();
	}
    
    public void setValues(Object[] values) {
		this.values = values;
	}

	public void setValue(Object value) {
        this.values = new Object[]{value};
    }

    public int getOperator() {
        return operator;
    }

    public void setOperator(int operator) {
        this.operator = operator;
    }
    
    public void setValueField(boolean valueField) {
		this.valueField = valueField;
	}

    /**
     * if the value is a field name 
     */
	public boolean isValueField() {
		return valueField;
	}

	private static int getOperator(MatchType matchType) {
    	int operator;
    	if (matchType == MatchType.EQ) {
    		operator = EQUALS;
    	} else if (matchType == MatchType.GE) {
    		operator = GE;
    	} else if (matchType == MatchType.GT) {
    		operator = GT;
    	} else if (matchType == MatchType.IN) {
    		operator = IN;
    	} else if (matchType == MatchType.LE) {
    		operator = LE;
    	} else if (matchType == MatchType.LIKE) {
    		operator = LIKE;
    	} else if (matchType == MatchType.LT) {
    		operator = LT;
    	} else if (matchType == MatchType.NEQ) {
    		operator = NOT_EQUALS;
    	} else if (matchType == MatchType.NIN) {
    		operator = NOT_IN;
    	} else {
    		throw new IllegalArgumentException("Unknow match type:" + matchType);
    	}
    	return operator;
    }
}
